<?php
namespace app\admin\controller;
use think\Db;
class Zx extends Base {

    public function index(){

        $count =Db::table('up_information_type')->count();
        if(request()->isAjax()){
            $page = input('page') ? input('page') : 1;
            $limits = input('limit') ? input('limit') : 10;// 获取总条数;
            $lists = Db::table('up_information_type')->alias('a')
                ->join('up_admin ua1', 'ua1.id = a.admin_id', 'left')
                ->join('up_admin ua2', 'ua2.id = a.update_admin_id', 'left')
                ->field('a.*,ua1.username,ua2.username as username1')
                ->page($page, $limits)
                ->order('a.information_type_id desc')
                ->select();
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        $this->assign([
            'count' => $count
        ]);
        return $this->fetch();
    }

    public function add(){
        if(request()->isAjax()){
            $data['information_type_name'] = input('param.title');

            $id_i = Db::table('up_information_type')->where('information_type_name',input('param.title'))->value('information_type_id');
            if($id_i){
                return ['code' => 100, 'data' => '', 'msg' => '分类名称已被使用'];
            }
            $data['create_time'] = date('Y-m-d H:i:s',time());
            $data['admin_id'] = session('uid');
            $result =Db::table('up_information_type')->insertGetId($data);
            if($result){
                return ['code' => 200, 'data' =>"", 'msg' => '新建成功'];
            }else{
                return ['code' => 100, 'data' => '', 'msg' => '新建失败'];
            }

        }
        return $this->fetch();
    }

    /**
     *更新
     */
    public function update(){
        if(request()->isAjax()){
            $ids = input('param.id');
            $data['information_type_name'] = input('param.title');
            $where['information_type_name'] =input('param.title');
            $where['information_type_id'] =array('NEQ',$ids);
            $id_i = Db::table('up_information_type')->where($where)->value('information_type_id');
            if($id_i){
                return ['code' => 100, 'data' => '', 'msg' => '分类名称已被使用'];
            }
            $data['update_time'] = date('Y-m-d H:i:s',time());
            $data['update_admin_id'] = session('uid');
            $list = Db::table('up_information_type')->where('information_type_id',$ids)->update($data);
            if($list){
                return ['code' => 200, 'data' =>"", 'msg' => '编辑成功'];
            }else{
                return ['code' => 100, 'data' => '', 'msg' => '编辑失败'];
            }
        }
        $id = input('param.id');
        $res = Db::table('up_information_type')->where('information_type_id',$id)->find();
        $this->assign('res',$res);
        return $this->fetch();
    }

    public function delete(){
        $id = input('ids');
        $where['information_type_id'] = $id;
        $ids = Db::table('up_information')->where($where)->value('information_id');
        if($ids){
            return ['code' => 100, 'data' => '', 'msg' => '该类型已被占用，不允许删除'];
        }
        $res = Db::table('up_information_type')->where('information_type_id',$id)->delete();
        if($res){
            return ['code' => 200, 'data' =>"", 'msg' => '删除成功'];
        }else{
            return ['code' => 100, 'data' => '', 'msg' => '删除失败'];
        }
    }
}